package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.Result;
import com.itheima.domain.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/9/29 14:38
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            // 获取文件的名称，判断其是xls还是xlsx
            String filename = excelFile.getOriginalFilename();
            Workbook workbook = null;
            if (filename.endsWith("xls")) {
                workbook = new HSSFWorkbook(excelFile.getInputStream());
            } else if (filename.endsWith("xlsx")) {
                workbook = new XSSFWorkbook(excelFile.getInputStream());
            } else {
                return new Result(false, "文件格式不正确,请检查");
            }
            //获取工作簿中的sheet
            Sheet sheet = workbook.getSheetAt(0);
            //获取sheet中的row
            int lastRowNum = sheet.getLastRowNum();
            List<OrderSetting> list = new ArrayList<>();
            for (int i = 1; i <= lastRowNum; i++) {
                OrderSetting orderSetting = new OrderSetting();
                Row row = sheet.getRow(i);
                Date date = row.getCell(0).getDateCellValue();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String format = sdf.format(date);
                orderSetting.setOrderDate(sdf.parse(format));
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                //double number = row.getCell(1).getNumericCellValue();
                String number = row.getCell(1).getStringCellValue();
                orderSetting.setNumber(Integer.parseInt(number));
                list.add(orderSetting);
            }
            orderSettingService.add(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


    //根据月份查询对应的预约设置数据
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    //预约设置;
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
